//
//  ThemeViewController.m
//  PTravel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "ThemeViewController.h"

@interface ThemeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (retain, nonatomic)UITableView *myTableView;
@property (retain, nonatomic)NSMutableArray *arr;
@end

@implementation ThemeViewController

- (void)dealloc
{
    [_url release];
    [_myTableView release];
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, BOUNDS.size.height) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
}

#pragma mark === 数据请求
- (void)getData
{
    self.arr = [NSMutableArray array];
    [AppTools getAFN:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88254153730504&lon=121.5396968842807&page=1&track_app_channel=App%2520Store&track_app_version=6.8.2&track_device_info=iPhone7%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%25209.1&v=1" block:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            [self.arr addObject:[dic objectForKey:@"photo"]];
        }
        [self.myTableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, BOUNDS.size.width-8, BOUNDS.size.height/3)];
    NSURL *url = [NSURL URLWithString:[self.arr objectAtIndex:indexPath.row]];
    [image sd_setImageWithURL:url placeholderImage:IMAGE];
    [cell addSubview:image];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return BOUNDS.size.height/3+8;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
